<html>
    <head></head>
    <body>
        用户名：<input type="text" id="username" />
        密码：  <input type="password" id="password" />
        <input type="button" id="submitBtn" value="提交" />
    </body>
    <script>
        Function.prototype.before = function(beforeFn) {
            const _self = this;
            return function() {
                if (!beforeFn.apply(this, arguments)) {
                    return;
                }
                return _self.apply(this, arguments);
            }
        }

        const username = document.getElementById('username');
        const password = document.getElementById('password');
        const submitBtn = document.getElementById('submitBtn');

        const validData = function () {
            if (username.value === '') {
                alert('用户名不能为空');
                return false;
            }
            if (password.value === '') {
                alert('密码不能为空');
                return false;
            }
            return true;
        }

        let formSubmit = function () {
            if (!validData()) {
                return;
            }

            const param = {
                username,
                password
            }
            ajax('/login', param)
        }

        formSubmit = formSubmit.before(validData);

        submitBtn.onclick = function () {
            formSubmit();
        }
    </script>
</html>